﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Specialized;
using System.Configuration;

namespace OpenPhotoAlbum
{
    public abstract class ConfigReader
    {
        private string _sectionName;
        private NameValueCollection _nameValueCollection;

        protected ConfigReader(string sectionName)
        {
            _sectionName = sectionName;
        }

        protected string ReadConfigValue(string key)
        {
            return ConfigItems[key];
        }

        protected NameValueCollection ReadConfigItems()
        {
            return ConfigItems;
        }

        private NameValueCollection ConfigItems
        {
            get
            {
                if (_nameValueCollection == null)
                {
                    _nameValueCollection = (NameValueCollection)ConfigurationManager.GetSection(_sectionName);
                }
                return _nameValueCollection;
            }
        }
    }

    public class FilterConfigReader : ConfigReader
    {
        private const string NAME_OF_FILTER_SECTION = "filtersSection";
        private const string KEY_OF_GENERAL_FILTER = "GeneralFilter";
        private const string KEY_OF_USERNAME_FILTER = "UserNameFilter";
        private const string KEY_OF_PASSWORD_FILTER = "PasswordFilter";

        #region Singleton

        private static readonly FilterConfigReader _instance = new FilterConfigReader();

        static FilterConfigReader()
        {
        }

        public static FilterConfigReader Instance
        {
            get { return _instance; }
        }

        #endregion

        private FilterConfigReader()
            : base(NAME_OF_FILTER_SECTION)
        {
        }

        public string ReadGeneralFilter()
        {
            return ReadConfigValue(KEY_OF_GENERAL_FILTER);
        }

        public string ReadUserNameFilter()
        {
            return ReadConfigValue(KEY_OF_USERNAME_FILTER);
        }

        public string ReadPasswordFilter()
        {
            return ReadConfigValue(KEY_OF_PASSWORD_FILTER);
        }

        public string ReadCustomFilter(string key)
        {
            return ReadConfigValue(key);
        }
    }

    public class EmailConfigReader : ConfigReader
    {
        private const string NAME_OF_EMAIL_SECTION = "emailSection";
        private const string KEY_OF_SMTP_SERVER_ADDRESS = "SmtpServerAddress";
        private const string KEY_OF_SMTP_SERVER_PORT = "SmtpServerPort";
        private const string KEY_OF_USER_NAME = "UserName";
        private const string KEY_OF_PASSWORD = "Password";
        private const string KEY_OF_FROM = "From";
        private const string KEY_OF_ENABLE_SSL = "EnableSsl";
        private const string KEY_OF_ADMIN_EMAIL = "AdminEmail";

        private EmailConfigReader()
            : base(NAME_OF_EMAIL_SECTION)
        {
        }

        #region Singleton

        private static readonly EmailConfigReader _instance = new EmailConfigReader();

        static EmailConfigReader()
        {
        }

        public static EmailConfigReader Instance
        {
            get { return _instance; }
        }

        #endregion

        public string ReadSmtpServerAddress()
        {
            return ReadConfigValue(KEY_OF_SMTP_SERVER_ADDRESS);
        }

        public int ReadSmtpServerPort()
        {
            return int.Parse(ReadConfigValue(KEY_OF_SMTP_SERVER_PORT));
        }

        public string ReadUserName()
        {
            return ReadConfigValue(KEY_OF_USER_NAME);
        }

        public string ReadPassword()
        {
            return ReadConfigValue(KEY_OF_PASSWORD);
        }

        public string ReadFrom()
        {
            return ReadConfigValue(KEY_OF_FROM);
        }

        public bool ReadEnableSsl()
        {
            return bool.Parse(ReadConfigValue(KEY_OF_ENABLE_SSL));
        }

        public string ReadAdminEmail()
        {
            return ReadConfigValue(KEY_OF_ADMIN_EMAIL);
        }
    }
}
